package meconsea.life.cms.ui;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import meconsea.life.cms.accessor.SArticleAccessor;
import meconsea.life.cms.accessor.VArticleAccessor;
import meconsea.life.cms.accessor.XArticleAccessor;
import meconsea.life.cms.accessor.YArticleAccessor;
import meconsea.life.cms.accessor.ZArticleAccessor;
import meconsea.life.cms.entity.VArticle;
import meconsea.life.cms.util.CMSUtil;
import meconsea.life.util.Constant;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.json.JSONArray;
import org.json.JSONObject;

import com.google.inject.Inject;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 网站首页的IndexAction <br>
 * 主要采用Ajax方式(response采用Json方式传输)
 * 
 * @author Administrator
 * 
 */
public class IndexAction extends ActionSupport {
	public final static Logger log = Logger.getLogger(IndexAction.class);
	// 头版头条列表(网站的最新文章)
	private final static String HEADER = "header";
	// 获取文章列表
	private final static String ARTICLE_LIST = "artList";
	// 文章的属性
	private final static String SQL_PRE_ARTICLE_PRO = "SELECT O.id,O.title,O.subtitle,O.keywords,O.summary,O.specialId,O.artClass,O.artClassName,O.artClassPath,O.indexImg,O.homeImg,O.artResource,O.author,O.commend,O.top,O.hot,O.comment,O.clicks,O.statusFlg,O.auditLevel,O.htmlPath,O.isHeader,O.createdAt,O.changedAt ";
	
	@Inject
	private VArticleAccessor vArticleAccessor;
	@Inject
	private YArticleAccessor yArticleAccessor;
	@Inject
	private SArticleAccessor sArticleAccessor;
	@Inject
	private ZArticleAccessor zArticleAccessor;
	@Inject
	private XArticleAccessor xArticleAccessor;
	//
	// 业务属性
	// 文章分类
	private String clsCode;
	//是否包括子类
	private String isChild;

	// 文章的类型
	private String artType;
	// 起始记录
	private int first;
	// 最大记录
	private int max;
	// 最热
	private int hot;
	// 置顶
	private int top;
	// 推荐
	private int commend;
	//头条
	private int isHeader;
	// 排序
	private String order;
	private String call;
	
	
	public String execute() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		response.setContentType("text/xml");
		response.setHeader("Cache-Control", "no-cache");
		response.setCharacterEncoding("UTF-8");
		PrintWriter pw = null;
		StringBuffer r_sb = new StringBuffer("");
		if (ARTICLE_LIST.equals(getCall())) {
			String querySql = SQL_PRE_ARTICLE_PRO;
			String whereSql = " WHERE 1=1";
			String orderSql = "";
			//文章分类
			if("1".equals(isChild)){//含子节点
				whereSql += " AND O.artClassPath like '"+clsCode+"|%' ";
			}else if(clsCode != null && clsCode.length()>1){//默认的是不包含子节点
				whereSql += " AND O.artClass = '"+clsCode+"'";
			}
			//hot
			if(hot != 0){
				whereSql += " AND O.hot = "+hot;
			}
			//commend
			if(commend != 0){
				whereSql += " AND O.commend = "+commend;
			}
			//top
			if(top != 0){
				whereSql += " AND O.top = "+top;
			}
			//isHeader
			if(isHeader != 0){
				whereSql += " AND O.isHeader = "+isHeader;
			}
			
			//order
			if(order != null && order.length() > 1){
				orderSql = " ORDER BY "+order;
			}
			if(orderSql.length()>1){
				orderSql += " ,O.changedAt ";
			}else{
				orderSql += " ORDER BY O.changedAt ";
			}
			if(whereSql.endsWith("1=1")){
				whereSql="";
			} 
			List la = null;
			if(Constant.ART_TYPE_ALL.equals(artType)){
				querySql += "  ,O.artType  FROM VArticle  O " + whereSql + orderSql;
				la = vArticleAccessor.getEntitys(querySql,first,max, null);
			}else if(Constant.ART_TYPE_S.equals(artType)){
				querySql += " FROM SArticle O " + whereSql + orderSql;
				la = sArticleAccessor.getEntitys(querySql,first,max, null);
			}else if(Constant.ART_TYPE_X.equals(artType)){
				querySql += " FROM XArticle O " + whereSql + orderSql;
				la = xArticleAccessor.getEntitys(querySql,first,max, null);
			}else if(Constant.ART_TYPE_Y.equals(artType)){
				querySql += " FROM YArticle O " + whereSql + orderSql;
				la = yArticleAccessor.getEntitys(querySql,first,max, null);
			}else if(Constant.ART_TYPE_Z.equals(artType)){
				querySql += " FROM ZArticle O " + whereSql + orderSql;
				la = zArticleAccessor.getEntitys(querySql,first,max, null);
			}
			JSONArray ja = new JSONArray();
			try{
				ja = CMSUtil.parseArtToJSON(la,false);
				JSONObject res = new JSONObject();
				res.put("result", "1");
				res.put("msg", ja);
				r_sb.append(res.toString());
			}catch(Exception e){
				log.error("解析结果出现异常",e);
				r_sb.append("{\"result\":\"0\"");
				r_sb.append(",\"msg\":\"解析结果集失败！\"}");
			}
		} else if (HEADER.equals(getCall())) {
			List lv_header = vArticleAccessor.getEntitysNamed(VArticle.HEADER_ART_CLASS,first,max, new Object[]{"isHeader",Constant.ART_HEADER});
			JSONArray ja = new JSONArray();
			try{
				ja = CMSUtil.parseArtToJSON(lv_header,false);
				JSONObject res = new JSONObject();
				res.put("result", "1");
				res.put("msg", ja);
				r_sb.append(res.toString());
			}catch(Exception e){
				log.error("解析结果出现异常",e);
				r_sb.append("{\"result\":\"0\"");
				r_sb.append(",\"msg\":\"解析结果集失败！\"}");
			}
		}else{
			r_sb.append("{\"result\":\"0\"");
			r_sb.append(",\"msg\":\"参数请求失败！\"}");
		}
		try{
			pw = response.getWriter();
			pw.write(r_sb.toString());
		}catch(Exception e){
			log.error("返回文章出现异常：",e);
			pw.write("{\"result\":\"0\",\"msg\":\""+e.getMessage()+"\"}");
		}
		return null;
	}
	
	

	public String getClsCode() {
		return clsCode;
	}

	public void setClsCode(String clsCode) {
		this.clsCode = clsCode;
	}

	public String getIsChild() {
		return isChild;
	}

	public void setIsChild(String isChild) {
		this.isChild = isChild;
	}
	public String getArtType() {
		return artType;
	}

	public void setArtType(String artType) {
		this.artType = artType;
	}

	public int getFirst() {
		return first;
	}

	public void setFirst(int first) {
		this.first = first;
	}

	public int getMax() {
		return max;
	}

	public void setMax(int max) {
		this.max = max;
	}

	public int getHot() {
		return hot;
	}

	public void setHot(int hot) {
		this.hot = hot;
	}

	public int getTop() {
		return top;
	}

	public void setTop(int top) {
		this.top = top;
	}

	public int getCommend() {
		return commend;
	}

	public void setCommend(int commend) {
		this.commend = commend;
	}

	public String getOrder() {
		return order;
	}

	public void setOrder(String order) {
		this.order = order;
	}

	public String getCall() {
		return call;
	}

	public void setCall(String call) {
		this.call = call;
	}

	public int getIsHeader() {
		return isHeader;
	}

	public void setIsHeader(int isHeader) {
		this.isHeader = isHeader;
	}

}
